////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                   PEER 2 PEER FILE SHARING SYSTEM                          //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
package com.cse.p2p.localfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.cse.p2p.common.*;

/**
 *
 * @author trunghieu
 */
public abstract class LocalFile {

	public static boolean isFileExist( String filename ) {
		File file = new File( filename );
		return file.exists();
	}

	public static boolean isPieceExist( String filename, int pieceNumber, int pieceSize ) {
		return getPiece( filename, pieceNumber, pieceSize ) != null;
	}

	public static byte[] getPiece( String filename, int pieceNumber, int pieceSize ) {
		try {
			File file = new File( filename );
			InputStream is = new FileInputStream( file );
			byte[] bytes = new byte[ pieceSize ];
			int i = 0;
			int length = 0;
			while ( i <= pieceNumber ) {
				length = is.read( bytes );
                i++;
			}
			if ( length <= 0 ) {
				return null;
			}
			return bytes;
		}
		catch ( FileNotFoundException fEx ) {
			System.err.println( "FileNotFoundException: " );
			System.err.println( fEx.getMessage() );
			return null;
		}
		catch ( IOException ioEx ) {
			System.err.println( "IOException: " );
			System.err.print( ioEx.getMessage() );
			return null;
		}
	}

//    public static String writeFiletoDisk(DicFile dicFile){
//        String path = null;
//        return path;
//    }

    public static boolean writeFiletoDisk(String path, byte[] file){
        boolean f = false;
        return f;
    }
}
